package com.ivsign.lib_idcardidentification;

import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;

import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;

/**
 * @description :
 * @autHor :  Jason
 * @date : 2018/8/15 19:22
 */

public class IDCardIdentificationManager implements IBlueToothAdapter {


    private BLEBlueToothAdapter bleBlueToothAdapter;

    private IDCardIdentificationManager() {
    }

    public void disconnect() {
        if (bleBlueToothAdapter != null) {

            bleBlueToothAdapter.disconnect();
        }
    }

    private static class BleManagerHolder {

        private static final IDCardIdentificationManager instance = new IDCardIdentificationManager();

    }
    public static IDCardIdentificationManager getInstance(){
        return BleManagerHolder.instance;
    }


    @Override
    public void init(Application context) {
        bleBlueToothAdapter = new BLEBlueToothAdapter();
        bleBlueToothAdapter.init(context);
    }

    @Override
    public void scan(BleScanCallback callback) {
        Intent discoverableIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//使得蓝牙处于可发现模式，持续时间150s
        discoverableIntent.putExtra(
                BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 150);
        bleBlueToothAdapter.scan(callback);
    }



    @Override
    public void connect(String mac, BleGattCallback bleGattCallback) {
        Intent discoverableIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);//使得蓝牙处于可发现模式，持续时间150s
        discoverableIntent.putExtra(
                BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 150);
        bleBlueToothAdapter.connect(mac,bleGattCallback);
    }



    @Override
    public void readId(BleDevice bleDevice, DeviceUUID uuid, IReadListener mIReadListener, Context context) {
        bleBlueToothAdapter.readId(bleDevice,uuid,mIReadListener,context);
    }

    @Override
    public void reset() {
        bleBlueToothAdapter.reset();
    }


}
